package com.applets.web.controller.jijin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.applets.common.utils.HttpUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * DanJuanZuHeController
 *
 * @author lufeixia
 * @version 1.0
 * @date 2021/8/16 16:31
 */
public class DanJuanZuHeController {

    public static void main(String[] args) throws InterruptedException {
        DanJuanZuHeController controller = new DanJuanZuHeController();
        // controller.test();
        // System.out.println(controller.jiJinXinXi("004753,050026,001595,009860,320007,002190,006060"));
        JSONArray csi1019 = null;
        try {
            csi1019 = controller.danjuan("CSI1019");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(csi1019);
    }

    @GetMapping("/danjuan")
    @ApiOperation(value = "蛋卷")
    public JSONArray danjuan(String code) throws IOException {
        JSONArray result = new JSONArray();

        String[] split = code.split(",");
        for (String code1 : split) {

            String url = "https://danjuanapp.com/djapi/holding/plan/" + code1;
            JSONObject param = new JSONObject();
            String cookie = "device_id=web_SJ0sVbkEd; accesstoken=2400100002dbe768c93d5098e5817e082c2458fe9305fafe8; refreshtoken=24001000003b2ce10bf29ae6d1717b5ff76c3780e645687ec; u=579527679; uid=579527679; channel=1300100141; xq_a_token=6a56579f40edefe33cdfa961a04043d21e74a35e; acw_tc=2760820a16273689939183813eb39b90552cd7d62398a6fe59f4eec930224d; timestamp=1627370376069";
            Map<String, String> headers = new HashMap<>();
            headers.put("Cookie", cookie);
            String formResult1 = HttpUtil.httpsGet(url, param, headers);

            result = JSONObject.parseObject(formResult1).getJSONObject("data").getJSONArray("items");
            url = "https://danjuanapp.com/djapi/fund/estimate-nav/";
            for (Object o : result) {
                JSONObject o1 = (JSONObject) o;
                String fd_code = o1.getString("fd_code");
                formResult1 = HttpUtil.httpsGet(url + fd_code, param, headers);
                JSONArray a1 = JSONObject.parseObject(formResult1).getJSONObject("data").getJSONArray("items");
                System.out.println(o1.getString("fd_name"));
                if (a1.size() > 0) {
                    JSONObject o2 = (JSONObject) a1.get(a1.size() - 1);
                    ((JSONObject) o).put("guzhi", o2.getString("nav"));
                }
            }

            return result;
        }
        return null;
    }
}
